<template>
  <div class="add_user_wrap">
    <el-button type="primary" size="small" @click="pop_show = true"
      >新增商品</el-button
    >
    <el-dialog
      title="新增商品"
      :visible.sync="pop_show"
      append-to-body
      width="60%"
    >
      <el-form
        :model="skuForm"
        :inline="true"
        status-icon
        ref="userForm"
        label-width="100px"
      >
        <el-form-item label="名称:" prop="name">
          <el-input
            type="text"
            v-model="skuForm.name"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="副标题:" prop="describe">
          <el-input
            type="text"
            v-model="skuForm.describe"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格:" prop="price">
          <el-input
            type="text"
            v-model="skuForm.price"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="进价:" prop="cost_price">
          <el-input
            type="text"
            v-model="skuForm.cost_price"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="市场价:" prop="market_price">
          <el-input
            type="text"
            v-model="skuForm.market_price"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="库存:" prop="stock">
          <el-input
            type="text"
            v-model="skuForm.stock"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类:" prop="category_id">
          <el-select v-model="skuForm.category_id" size="small">
            <el-option
              v-for="item in category_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上下架:" prop="is_selling">
          <el-radio v-model="skuForm.is_selling" label="true">是</el-radio>
          <el-radio v-model="skuForm.is_selling" label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="SPU" prop="spu_id">
          <el-select
            v-model="skuForm.spu_id"
            size="small"
            @change="skuForm.specs = {}"
          >
            <el-option
              v-for="item in spus_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item v-for="item in specs_list" :label="item.name" :key="item.id">
            <el-select v-model="skuForm.specs[item.id]" size="small">
              <el-option
                v-for="opt in item.options"
                :key="opt.id"
                :label="opt.value"
                :value="opt.id"
                >
              </el-option>
            </el-select>
          </el-form-item><br> -->
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import cons from "@/components/constant";
let token = localStorage.token;
export default {
  name: "AddUser",
  data() {
    return {
      pop_show: false,
      skuForm: {
        name: "",
        spu_id: "",
        describe: "",
        category_id: "",
        price: "",
        cost_price: "",
        market_price: "",
        stock: "",
        is_selling: "",
        specs: {},
      },
      category_list: [],
      spus_list: [],
      // specs_list:[],
    };
  },
  methods: {
    // 获取所有的类别
    fnGetCategory() {
      this.axios
        .get(cons.apis + "/goods/channels/firstCate/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          this.category_list = dat.data;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    // 获取所有的spu
    fnGetSpus() {
      this.axios
        .get(cons.apis + "/goods/spus/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          this.spus_list = dat.data;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },

    // 点击提交触发得分方法
    submitForm() {
      //  var specs_sub_list = [];
      //  for(var k in this.skuForm.specs){
      //     specs_sub_list.push({'spec_id':k,'option_id':this.skuForm.specs[k]})
      //  }
      this.axios
        .post(
          cons.apis + "/goods/skus/",
          {
            name: this.skuForm.name,
            spu: this.skuForm.spu_id,
            describe: this.skuForm.describe,
            cate: this.skuForm.category_id,
            price: this.skuForm.price,
            // cost_price: this.skuForm.cost_price,
            // market_price: this.skuForm.market_price,
            stock: this.skuForm.stock,
            is_selling: this.skuForm.is_selling,
          },
          {
            headers: {
              Authorization: "JWT " + token,
            },
            responseType: "json",
          }
        )
        .then((dat) => {
          console.log('添加商品的响应:', dat)
          if (dat.status == 201) {
            this.$message({
              type: "success",
              message: "商品添加成功!",
            });
            this.pop_show = false;
            // this.skuForm.name = "";
            // this.skuForm.spu_id = "";
            // this.skuForm.describe = "";
            // this.skuForm.category_id = "";
            // this.skuForm.price = "";
            // this.skuForm.cost_price = "";
            // this.skuForm.market_price = "";
            // this.skuForm.stock = "";
            // this.skuForm.is_selling = "";
            this.skuForm.specs = {};
            this.$refs['userForm'].resetFields()
            this.$emit("fnResetTable");
          }
        })
        .catch((err) => {
          if (err.response.status == 400) {
            this.$message({
              type: "info",
              message: err.response.data.name[0],
            });
          }
        });
    },
    // resetForm() {
    //   this.skuForm.name = "";
    //   this.skuForm.spu_id = "";
    //   this.skuForm.describe = "";
    //   this.skuForm.category_id = "";
    //   this.skuForm.price = "";
    //   this.skuForm.cost_price = "";
    //   this.skuForm.market_price = "";
    //   this.skuForm.stock = "";
    //   this.skuForm.is_selling = "";
    //   this.skuForm.specs = {};
    // },
  },
  computed: {
    look_good_id() {
      return this.skuForm.spu_id;
    },
  },
  mounted() {
    this.fnGetCategory();
    this.fnGetSpus();
  },
  watch: {
    // look_good_id(newVal,oldVal){
    //    this.fnGetSpecs();
    // }
  },
};
</script>

<style scoped>
.input_width {
  width: 450px;
}
</style>
